package com.songlanyun.modules.account.model.dto;

import cn.afterturn.easypoi.excel.annotation.Excel;
import cn.afterturn.easypoi.handler.inter.IExcelDataModel;
import cn.afterturn.easypoi.handler.inter.IExcelModel;
import com.songlanyun.common.enums.MemberConstant;
import com.songlanyun.common.utils.ShareCodeUtils;
import com.songlanyun.modules.rabbitmq.util.SpringContextUtil;
import com.songlanyun.modules.shop.entity.ShopEntity;
import com.songlanyun.modules.shop.service.ShopService;
import lombok.Data;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.hibernate.validator.constraints.Length;

import javax.validation.constraints.Max;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.Date;
import java.util.Objects;

/**
 * 导入会员
 */
@Data
public class ImportUserDTO implements IExcelDataModel, IExcelModel {

    /**
     * 行号
     */
    private int rowNum;
    /**
     * 错误消息
     */
    private String errorMsg;

    @Excel(name = "会员ID*")
    @NotNull(message = "[会员ID]不能为空")
    private Long userId;

    @Excel(name = "会员昵称")
    private String nickname;

    @Excel(name = "会员手机号*")
    @Length(max = 11)
    private String mobile;

    public String getMobile() {
        //如果手机号为空，则设为会员id
        if (StringUtils.isBlank(this.mobile)
                || !isValidMobileNumber(this.mobile)) {
            this.mobile = this.getUserId().toString();
        }
        return this.mobile;
    }

    @Excel(name = "会员等级*")
    @NotNull(message = "[会员ID]不能为空")
    private Integer level;

    @Excel(name = "推荐人ID*")
    private Long parentUserId;

    @Excel(name = "所有父级推荐人的ID链*")
    private String parentPath;

    /**
     * 邀请码
     */
    private String invitationCode;

    //生成邀请码
    public String getInvitationCode() {
        String mobile = this.getMobile();
        if (mobile.length() > 11) {
            System.out.println();

        }
        String code = ShareCodeUtils.idToCode(Long.parseLong(this.getMobile()));
        this.invitationCode = code;
        return code;
    }

    @Override
    public void setRowNum(Integer var1) {
        this.rowNum = var1;
    }


    public Integer getRowNum() {
        return this.rowNum;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        ImportUserDTO that = (ImportUserDTO) o;
        return this.mobile != null && Objects.equals(that.mobile, this.mobile);
    }

    @Override
    public int hashCode() {
        return Objects.hash(mobile);
    }

    @Override
    public String toString() {
        return ToStringBuilder.reflectionToString(this);
    }

    boolean isValidMobileNumber(String s) {
        return s.matches("\\d{11}");
    }
}
